#ifndef _font
#define _font

#include <vector>
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <GL/gl.h>
using namespace std;

class font {

   string name;

   int nchars;

   int xcells, ycells;

   int cellw, cellh;
   int charw, charh;

   map < char, vector<double> >  characters; // maps char -> character data


   public:


      font () {
      }

      font (const string& filename, int cw, int ch) {
         ifstream file (filename.c_str(), ios::in);
         load (file);
         setSize (cw, ch);
      }

			void load (ifstream& file) {

				string ignore;
				file >> ignore >> name;

				file >> ignore >> nchars;

				file >> ignore >> xcells >> ignore >> ycells;

				for (int i = 0; i < nchars; i++) {

						char c;
						int npoints;

						file >> c >> npoints;

						vector<double>& chardesc = characters [c];
						chardesc.push_back (npoints);

						for (int j = 0; j < npoints; j++) {

							 double cx, cy; file >> cx >> cy;
							 chardesc.push_back (cx);
							 chardesc.push_back (cy);

						}

				}

				vector<double> vs; vs.push_back (0);
				characters[' '] = vs;
				++nchars;
			}

      int charWidth () const { return charw; }
      int charHeight() const { return charh; }

      void draw_char (char c, int x, int y, int z=0) {
         const vector<double>& chardesc = characters [c];
         if (chardesc.size()) {
            glBegin (GL_LINES);
            for (int i = 0, j = (int) chardesc [0], k = 1; i < j; i++, k+=2) {
               glVertex3i (x + (int) (chardesc[k] * cellw), y + (int)(chardesc[k+1] * cellh), z);
            }
            glEnd ();
         }

      }

      bool isCharAvailable (char c) {
         const vector<double>& chardesc = characters [c];
         if (chardesc.size()) return true; else return false;
      }

      void setSize (int cw, int ch) {

         charw = cw;
         charh = ch;

         cellw = charw / xcells;
         cellh = charh / ycells;


      }

      /*int stringWidth (const string& s) { return charWidth () * s.length(); }
      int stringWidth (int nchar) { return charWidth () * nchar; }
      int stringHeight () { return charHeight(); }*/

};

extern font fnt;

inline font& get_font () {
	return fnt;
}
inline int get_char_height () {
	return fnt.charHeight ();
}
inline int get_char_width () {
	return fnt.charWidth ();
}

inline void draw_string (const string& s, int x, int y, int z=0) {
	font& f = get_font ();
  int cw = f.charWidth ();
  for (int p = 0, q = s.length(); p < q; p++) {
      f.draw_char (s[p], x, y, z);
      x += cw;
  }
}

#endif
